<template>
  <el-dialog :title="'新增测评人员'" :close-on-click-modal="false" :visible.sync="visible" @close="clearDataList()" width="90vw" top="5vh">
    <el-container>
      <el-main style="height:70vh;overflow-x: hidden;">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="24" :md="5">
            <div class="boxsty">
              <el-tree :data="treeData" :props="defaultProps" :filter-node-method="filterNode" class="filter-tree" node-key="id" highlight-current :default-expanded-keys="[1]" accordion @node-click="getNodeData" />
            </div>
          </el-col>

          <el-col :xs="24" :sm="24" :md="7">
            <div class="boxsty">
              <div class="flex titbottom">
                <el-input v-model="listQuery.name" style="margin-right:10px" placeholder="请输入搜索用户"></el-input>
                <el-button @click="btnSearch" type="primary" icon="el-icon-search" plain>搜索</el-button>
              </div>
              <el-table @row-click="handleClick" :data="tableDataDept" v-loading="tableloading" style="width: 100%">
                <el-table-column prop="username" label="用户">
                </el-table-column>
                <el-table-column prop="ryName" label="姓名">
                </el-table-column>
                <!-- <el-table-column fixed="right" label="操作" width="100">
                   <template slot-scope="scope">
              <el-button @click="handleClick(scope.row)" type="text" size="small">添加</el-button>
            </template>
                </el-table-column>-->
              </el-table>
              <div style="margin-top:20px">
                <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="page.currentPage" :page-sizes="[10, 20, 50, 100]" :page-size="page.pageSize" :total="page.total" background layout="total, sizes, prev, pager, next">
                </el-pagination>
              </div>

            </div>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12">
            <div class="boxsty">
              <div class="tabletext titbottom">已选择人员</div>
<el-table :data="gzpzDataList" stripe>
                <!--<el-table-column fixed prop="userId" label="Id" width="80">-->
                <!--</el-table-column>-->
                <el-table-column prop="userName" label="测评人员" min-width="200">
                </el-table-column>
                <el-table-column prop="cpPs" label="评票票数" min-width="200">
                </el-table-column>

                <el-table-column  label="操作" width="100">
                  <template slot-scope="scope">
                    <el-button @click="removeUser(scope.$index,scope.row.id)" type="danger" plain>删除</el-button>

                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-col>
        </el-row>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" v-if="canSubmit"  @click="addBathUser">确定</el-button>
      <el-button type="primary" plain @click="btnCancel">取消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { fetchTree } from '@/api/admin/dept'
import { fetchList } from '@/api/admin/user'
import { getPzByGzIdAndMxId, addObj, delObj } from '@/api/ceping/cepingcpgzcpmxcppzb'
export default {
  data() {
    return {
      qjDeptId: '',//全局部门id
      tableloading: false,//table遮挡
      visible: false,
      canSubmit:false,
      tableDataDept: [],
      tableDataUser: [],
      dataForm: {
      },
      listQuery: {
        name: ''
      },
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条,
        isAsc: false// 是否倒序
      },
      searchForm: {},
      zdySearchForm: {},
      gzpzDataList: [],
      gzId: '',
      mxId: '',
      tempList: []
    }
  },
  created() {
    this.getList();
  },
  methods: {
    init(gzid, mxid) {
      this.visible = true;
      this.canSubmit=true;
      this.gzId = gzid;
      this.mxId = mxid;
      //查询配置规则
      getPzByGzIdAndMxId(this.gzId, this.mxId).then(response => {
        this.gzpzDataList = response.data.data;
      }).catch(() => {
        this.$notify.closeAll();
        this.$notify.error("数据错误");
      });
    },
    getList() {
      fetchTree(this.listQuery).then(response => {
        this.treeData = response.data.data
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    getPersons(page, params) {
      this.tableloading = true;
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm, this.zdySearchForm)).then(response => {
        this.tableDataDept = response.data.data.records
        this.page.total = response.data.data.total
        this.tableloading = false;
      }).catch(err => {
        this.tableloading = false;
      })
    },
    getNodeData(data) {
      this.page.currentPage = 1
      this.qjDeptId = data.id;
      this.getPersons(this.page, { deptId: data.id })
    },
    //选人添加到右边的框
    handleClick(obj) {

      for (var i = 0; i < this.gzpzDataList.length; i++) {
        if (this.gzpzDataList[i].userId == obj.userId) {
          return
        }
      }
      this.gzpzDataList.push({
        gzId: this.gzId,
        cpId: this.mxId,
        userId: obj.userId,
        userName: obj.ryName,
        cpPs: 1
      })
      this.tempList.push({
        gzId: this.gzId,
        cpId: this.mxId,
        userId: obj.userId,
        userName: obj.ryName,
        cpPs: 1
      })
    },
    removeUser(index, id) {
      if (id) {
        delObj(id).then(response => {
          if (response.data.code=="0") {
            this.gzpzDataList.splice(index, 1)
          }
        }).catch(() => {
          this.$notify.closeAll();
          this.$notify.error("数据错误");
        });
      } else {
        this.gzpzDataList.splice(index, 1)
      }
    },
    //保持测评人员
    addBathUser() {
       this.canSubmit=false;
        if(this.gzpzDataList.length==0){
          this.$emit('refPzGzmx');
           this.visible = false
          return;
        }

      addObj(this.gzpzDataList).then(response => {
        this.canSubmit=true;
        if (response.data.code=="0") {
           this.$notify.closeAll();
          this.$notify.success('保存成功');
          this.tempList = [];
          this.visible = false
          this.$emit('refPzGzmx')
        } else {
          this.$notify.closeAll();
          this.$notify.error("数据错误");
          this.tempList = [];
        }
      }).catch(() => {
        this.$notify.closeAll();
        this.$notify.error("数据错误");
        this.tempList = [];
        this.canSubmit=true;
      });
    },
    //清空数据
    clearDataList() {
      this.tempList = [];
      this.gzpzDataList = [];
    },
    btnSearch() {
      let param = {
        deptId: this.qjDeptId
      }

      if (this.listQuery.name.trim() !== '') {
        param['username'] = this.listQuery.name.trim();
        param['ryName'] = this.listQuery.name.trim();
      }
      this.getPageUsers(param);

    },
    // 分页
    // 每页数
    sizeChangeHandle(val) {
      this.page.pageSize = val
      this.page.currentPage = 1
      this.getPageUsers()
    },
    // 当前页
    currentChangeHandle(val) {
      this.page.currentPage = val
      this.getPageUsers()
    },
    //分页刷新
    getPageUsers(vm) {
      if (vm) {
        this.getPersons(this.page, vm)
      } else {
        this.getPersons(this.page, { deptId: this.qjDeptId })
      }
    },
    //取消按钮
    btnCancel(){
      this.visible = false;
      this.clearDataList();
    }
  }
}
</script>

<style scoped>
</style>
